<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        include "ModelClass.php";


        //Funcion para crear las tablas dando un prefijo.
        function crearTablas($prefijo){
            $query= array(  "CREATE TABLE ".$prefijo."structure (
                                id INT(11) AUTO_INCREMENT PRIMARY KEY,
                                value VARCHAR(100) NOT NULL,
                                type VARCHAR(20) NOT NULL,
                                lft INT(6) NOT NULL,
                                rgt INT(6) NOT NULL
                            );",
                            //Indice sobre value
                            "CREATE UNIQUE INDEX IDX_STRUCT ON ".$prefijo."structure (value);",
                            //Tabla datos
                            "CREATE TABLE ".$prefijo."data(
                                id INT(11)NOT NULL,
                                id_structure INT(11)NOT NULL,
                                value VARCHAR(100)NOT NULL,
                                PRIMARY KEY(id,id_structure),
                                FOREIGN KEY (id_structure)
                                REFERENCES ".$prefijo."structure(id),
                                FOREIGN KEY (id)
                                REFERENCES ".$prefijo."elements(id)
                            );",
                            "CREATE TABLE ".$prefijo."elements(
                                type CHAR(20),
                                id INT (11) AUTO_INCREMENT PRIMARY KEY ,
                                name VARCHAR(50)
                            );",
                            "CREATE INDEX IDX_ELEMENTS ON ".$prefijo."elements(name) "
                            );

            foreach ($query as $q) DB::query($q);

        }
        
        
        function crearEstructura($model,$debug=false){
            $model->initializeStruct($debug);
            $model->addChild("ACTIVIDAD","element","","",$debug);
            $model->addChild("OBLIGATORIO","boolean","/ACTIVIDAD","",$debug);
        }


        function conecta($dbhost,$dbusername,$dbpass,$database){
            $link=DB::connect($dbhost,$dbusername,$dbpass);
            if (!$link) {
                die('Could not connect: ' . mysql_error());
                return FALSE;
            }
            if(!DB::select_db($database,$link)){
                    DB::close($link);
                    die('Could not connect: ' . mysql_error());
                    return FALSE;
            }
            return $link;
        }


        






        ?>
    </body>
</html>
